#include "image.h"

namespace SkyID
{

//- Image ---------------------------------------------------------------------

Image::Image() :
	mWidth(0), mHeight(0),
	mFormat(FORMAT_UNKNOWN),
	mData(NULL)
{
	//
}

Image::~Image()
{
	clean();
}

Image::Image(const Image &img)
{
	copyFromImage(img);
}

Image& Image::operator=(const Image &img)
{
	copyFromImage(img);
	return *this;
}

void Image::clean()
{
	delete[] mData;
	mData = NULL;
	mWidth = mHeight = 0;
	mFormat = FORMAT_UNKNOWN;
	setValid(false);
}

void Image::createImage(uint32 width, uint32 height, ImageFormat format)
{
	clean();
	mData = new uchar[width*height*format];
	LOG_ASSERT(mData);
	mWidth = width;
	mHeight = height;
	mFormat = format;
	setValid(true);
}

void Image::copyFromImage(const Image &img)
{
	if (this == &img)
		return;
	clean();
	if (!img.isValid())
		return;
	createImage(img.getWidth(), img.getHeight(), img.getFormat());
	LOG_ASSERT(img.getData());
	memcpy(mData, img.getData(), mWidth*mHeight*mFormat);
}

} // namespace SkyID
